package service;

import dao.MiddleDao;
import dao.MiddleDaoImpl;
import dao.RoleDao;
import dao.RoleDaoImpl;
import pojo.Role;

import java.util.List;

public class RoleServiceImpl implements RoleService{
    RoleDao roleDao = new RoleDaoImpl();
    MiddleDao middleDao = new MiddleDaoImpl();
    @Override
    public List<Role> selectALl(Integer pageIndex,Integer pageSize) {
        return roleDao.selectALl(pageIndex,pageSize);
    }

    @Override
    public int total() {
        return roleDao.total();
    }

    @Override  //事务问题
    public int addRole(String rolename, String state, String[] ids) {
        int k = 0;
        try {
            //1.新增角色表
            Role role = new Role();
            role.setRoleName(rolename);
            role.setRoleState(Integer.parseInt(state));
            int i = roleDao.addRole(role);
            //2.新增中间表
            //如何获得新增数据的id？
            middleDao.insertMiddle(i,ids);
            k=1;
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }

        return k;
    }

    @Override
    public Role findbyid(Integer roleid) {
        Role role = roleDao.findByid(roleid);
        return role;
    }

    @Override
    public int deletebyid(int roleid) {
        return roleDao.deletebyid(roleid);
    }

}
